package leetcode.all;

import java.util.HashMap;

public class Solution560_ {
    public int subarraySum(int[] nums, int k) {
        HashMap<Integer, Integer> sumAndCount = new HashMap<>();
        sumAndCount.put(0,1);
        int count = 0, curSum = 0;
        for (int i = 0; i < nums.length; i++) {
            curSum += nums[i];
            count += sumAndCount.getOrDefault(curSum-k,0);
            sumAndCount.put(curSum,sumAndCount.getOrDefault(curSum,0)+1);
        }
        return count;
    }
}
